<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_NEWYEAR.php' );
require_once( '../system/mdao/Class_M_NEWYEAR_SHOP.php' );
require_once( '../system/mdao/Class_M_NEWYEAR_WEEK.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_NEWYEAR,$login['staff_auth']);
$isG7  = FALSE;
if   ($login['staff_auth'] == 'G-7') {
	$isG7 = TRUE;
}
$smarty->assign('isG7', $isG7 );
$dao = new Class_M_NEWYEAR;
$daoS = new Class_M_NEWYEAR_SHOP($dao->get_db());
$daoW = new Class_M_NEWYEAR_WEEK();
if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["id"] =  $_GET['id'];
		$datas  = $daoS->getByMark( $_GET['id'] );
		foreach ($datas as $k=>$v) {
			$forms["mutirow"] []=array("shop_cd"=> $v["shop_cd"], "shop_name" => $v["shop_name"]);
		}
	}else{
		$forms["id"] = $_GET['id'];
		$dYear = $daoW->get(1);
		
		$forms["memo_day1"]  = $dYear["week1"];
		$forms["memo_day2"]  = $dYear["week2"];
		$forms["memo_day3"]  = $dYear["week3"];
		$forms["memo_day4"]  = $dYear["week4"];
	}

}else{
	$forms = $_POST;
	if (isset($forms["shop_cd"])) {
		foreach ($forms["shop_cd"] as $k=>$v) {
			$forms["mutirow"] []=array("shop_cd"=> $v, "shop_name" => $forms["shop_name"][$k]);
		}
	} 
	
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		
		//if( $forms['id'] == '' ) $err_mes['login_pwd']	= $err->check( $forms['login_pwd'], $chk['login_pwd'] );

		if( $err_mes['year_mark'] == '' ){
			
			if( $forms['id'] == '' ){
				$where = sprintf(" year_mark = '%s'", $forms['year_mark'] );
			}else{
				$where = sprintf(" year_mark = '%s' AND year_mark <> '%s'", $forms['year_mark'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['year_mark'] = '<p class="error">この記号は重複しています</p>';
			}
			
		}

		if ( $err->clear ){
			$cols     = $dao->get_cols();
			foreach($cols as $kcol => $val) {
				$values[$kcol] = $forms[$kcol] ;
			}

			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
		}
		

		$dbarray = $daoS->getByMark( $forms["id"] );

		//削除.更新のデータ作成
		foreach($dbarray as $k=>$v) {
			$delflg = TRUE;
			foreach($forms["mutirow"] as $kk=>$vv) {
				if ($v["shop_cd"] == $vv["shop_cd"] ) {
					$delflg = FALSE;
					break;
				}
			}
			if ($delflg){
				$datadel[]  = $v["shop_cd"];
			} else {
				$dataedit[] = $v["shop_cd"];
			}
		}
		
		//追加のデータ作成
		foreach($forms["mutirow"] as $k=>$v) {
			$taddflg = TRUE;
			foreach($dbarray as $kk=>$vv) {
				if ($v["shop_cd"] == $vv["shop_cd"] ) {
					$taddflg = FALSE;
					break;
				}
			}
			if ($taddflg){
				$dataadd[] = $v["shop_cd"];
			}
		}
		
		foreach($dataadd as $k=>$v) {
			
			$where = "shop_cd = '{$v}' ";
			 
			if ( $daoS->exits($where) ) {
				$err->clear = FALSE;
				$err_mes['shop_cd'] = '<p class="error">店舗コード('.$v.')は他の記号はすでに使用しています</p>';
				
			}
		}
			
		if( $err->clear ){
			$executeflg = TRUE  ;
			try {
				$dao->begin_trans() ;
				
				if( $forms['id'] == '' ){
					if ($dao->add0($values)) {
						
						//$forms['id'] = $id;
						$smarty->assign('message', '登録が完了しました。');
						
					} else {
						$smarty->assign('message', '登録が失敗しました。');
						$executeflg = FALSE;
					}
					
				} else {
					if ($dao->edit( $forms['id'], $values )) {
						$smarty->assign('message', '更新が完了しました。');
					} else {
						$smarty->assign('message', '更新が失敗しました。');
						
						$executeflg = FALSE;
					}
					
				}
				
				if  ($executeflg) {
					foreach ($datadel as $k=>$v) {
						if ($daoS->remove($v)) {
						} else {
							$executeflg = FALSE;
						}
					}
				
					foreach ($dataedit as $k=>$v) {
						$values = array();
						$values["year_mark"]      =  $forms["year_mark"];
						$values["shop_cd"]        =  $v;
						if ($daoS->edit( $v, $values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					
					foreach ($dataadd as $k=>$v) {
						$values = array();
						$values["year_mark"]      =  $forms["year_mark"];
						$values["shop_cd"]      =  $v;
						
						//$values["disp_ng"]          =  $disp_ng;
						
						if ($daoS->add0($values)) {
						} else {
							$executeflg = FALSE;
						}
					}

				}
				
				
				
				if  ($executeflg) {
					$dao->commit_trans();
					$forms['id'] = $forms['year_mark'];
				} else {
					$dao->rollback_trans();
				}
			} catch (Exception $e) {
				$dao->rollback_trans();
				$smarty->assign('message', 'ＤＢエラーで失敗しました。');
			}
			
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {
	
}

$smarty->assign('flags', $aoki_disp_flags );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mnewyear_form.html');
?>